<?php
if (!defined("__WD__")) exit("Wrong Connect");

class Admin extends __Controller {

	function __construct() {
		parent::__construct();

		$this->load->model( 'admin/admin', $this->admin );
		$this->data->make("admin");

		$this->data->admin->redactor	= false;
		
		$this->data->title		= "Wandu Administrator";
		$this->data->library	= "http://".HOME."/library";
		$this->data->href		= "http://".HOME."/admin";
		$this->data->ajax		= "http://".HOME."/admin/ajax";

	}

	function ajax() {
		
		$this->load->module( 'input' , $this->input );
		
		setHeader("json", "utf-8");

		$action = $this->input->post('action');
		
		$ret = 0;
		
		switch( $action ) {
			case "logout" : 
				$ret = $this->admin->logout();
				break;
			case "login" :
				$ret = $this->admin->login($this->input->post('id'), $this->input->post('password'));
				break;
		}

		echo json_encode( array(
			'action' => $action,
			'output' => $ret
		) );

		exit;
	}

	function __default() {

		$this->load->package("kong");
		
		setHeader();

		if ( ! $this->admin->auth() ) { $this->load->view("admin/login"); exit; }
		
		$parameter = func_get_args();

		if ( count($parameter) == 0 ) {  // Launcher
			
			$kong_path = __ROOT__."/kong";
			$dir = opendir($kong_path);

			while( ($dp = readdir($dir)) !== false ) {
				if ( !file_exists($kong_path."/".$dp."/config.php") ) continue;

				include $kong_path."/".$dp."/config.php";

				$icon = ( file_exists($kong_path."/".$dp."/icon.png") )?
					"http://".HOME."/kong/".$dp."/icon.png":
					$this->data->library."/grp/admin/img/icon.png";
				
				$lang = $this->data->config->wandu['language'];
				
				$this->data->config->kongs[$dp] = array(
					'id' => $dp,
					'name' => ( $config['name'][$lang] ) ? $config['name'][$lang] : $config['name']['default'],
					'icon' => $icon
				);

				unset($config, $lang);
			}
			ksort($this->data->config->kongs);

			closedir($dir);
			$this->load->view("admin/header");
			$this->load->view("admin/home");
			$this->load->view("admin/footer");
		
		}
		else {

			$stat = array_shift($parameter);

			$this->data->config->kong['path'] = $kong_path = __ROOT__."/kong/".$stat;
			$this->data->config->kong['id']	= $stat;

			try {
				$config_path = $kong_path."/config.php";
				if ( !file_exists($config_path) ) throw new Error("존재하지 않는 페이지입니다.");

				include $config_path;
				
				$this->data->config->kong = array_merge($this->data->config->kong, $config);
				
				$lang = $this->data->config->wandu['language'];
				
				$this->data->config->kong['name'] = ( $config['name'][$lang] ) ? $config['name'][$lang] : $config['name']['default'];
				
				$class_path = $kong_path."/kong.php";
				if ( !file_exists($class_path) ) throw new Error("존재하지 않는 페이지입니다.");
				require_once( $class_path );
				
				$class_name = $config['class'];
				if ( !class_exists($class_name) ) throw new Error("존재하지 않는 클래스입니다. ({$class_name})");
				$kongs = new $class_name;
				
				if ( !method_exists($kongs, "doit") ) throw new Error("존재하지 않는 메서드입니다. ({$class_name}::doit())");
				//call_user_func_array(array($kongs, "doit"), $parameter);
				$kongs->doit($parameter);

				unset($config);
			}
			catch (Error $e) {echo $e;exit;}
		}

		
	}
	function __finish( $context ) {
//		p($this->data);
//		exit;
		if ( $this->data->admin->redactor ) {
/*			
			$this->load->helper("url");
			$relative_root = url_relativeRoot();
*/			
			$library = $this->data->library;
			if ( !isset($this->data->head) ) $this->data->head = "";
			$this->data->head .= <<<EOD
<link rel="stylesheet" href="{$library}/grp/admin/css/redactor.css" />
EOD;
			if ( !isset($this->data->body) ) $this->data->body = "";
			$this->data->body .= <<<EOD
<script src="{$library}/js/jquery-migrate-1.1.0.min.js"></script>
<script src="{$library}/grp/admin/js/redactor.js"></script>
<script src="{$library}/grp/admin/js/redactor.langs/ko.js"></script>
<script src="{$library}/grp/admin/js/redactor.admin.js"></script>
EOD;
		}
		
		if ( isset($this->data->head) ) {
			$head = <<<EOD
{$this->data->head}

</head>
EOD;
			$context = str_replace("</head>", $head, $context);
			
		}
		if ( isset($this->data->body) ) {
			$body = <<<EOD
{$this->data->body}

</body>
EOD;
			$context = str_replace("</body>", $body, $context);
		}
		
		
		echo $context;
	}
}
